﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using BLL;
using DTO;

namespace GaraOto
{
    public partial class frmAddStaff : DevExpress.XtraEditors.XtraUserControl
    {
        StaffBLL staffBLL = new StaffBLL();
        UserBLL userBLL = new UserBLL();
        RoleBLL roleBLL = new RoleBLL();
        public frmAddStaff()
        {
            InitializeComponent();
            BindingDataStaff();
            ResetInfoStaff();
            BindingDataComboboxRole();
            BindingDataRole();
            BindingDataUser();
        }

        public void BindingDataStaff()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = staffBLL.SelectAll();
            bnvStaff.BindingSource = bs;
            gcStaff.DataSource = bs;
        }

        public void BindingDataRole()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = roleBLL.SelectAll();
            bnvRole.BindingSource = bs;
            gcRoles.DataSource = bs;

            //dua du lieu vao combobox
            cbeStatusRole.Properties.Items.Clear();
            cbeStatusRole.Properties.Items.Add(MessageAction.CapPhep);
            cbeStatusRole.Properties.Items.Add(MessageAction.HuyBo);
            cbeStatusRole.SelectedIndex = 0;
        }

        public void BindingDataComboboxRole()
        {
            if (roleBLL.DropListRole() != null)
            {
                leRoleName.Properties.DataSource = null;
                leRoleName.Properties.Columns.Clear();
                leRoleName.Properties.DataSource = roleBLL.DropListRole();
                leRoleName.Properties.DisplayMember = "RoleName";
                leRoleName.Properties.ValueMember = "RoleID";
                //leRoleName.Properties.Columns["RoleID"].Visible = false;
                //leRoleName.Properties.Columns["Status"].Visible = false;
                leRoleName.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("RoleName", 126));
                
            }

        }

        public void BindingDataUser()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = userBLL.SelectAll();
            bnvUser.BindingSource = bs;
            gcUsers.DataSource = bs;
        }

        private void textEdit1_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void comboBoxEdit2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            StaffDTO staffDTO = new StaffDTO();
            staffDTO.FirstName = teFirstName.Text;
            staffDTO.LastName = teLastName.Text;
            staffDTO.Address = teAddress.Text;
            staffDTO.Sex = cbeSex.SelectedItem.ToString();
            staffDTO.DateOfBirth = deBirth.DateTime;
            staffDTO.Tel = teTel.Text;
            staffDTO.Email = teEmail.Text;
            staffDTO.CMND = teCMND.Text;
            staffDTO.StartDate = deStartDate.DateTime;
            staffDTO.ImageProfile = peProfileImage.Image;
            staffDTO.Status = cbeStatus.SelectedItem.ToString();

            string message = staffBLL.Insert(staffDTO);
            if (message.Equals(MessageAction.Success))
            {
                MessageBox.Show("Thêm mới nhân viên thành công");
                BindingDataStaff();
            }
            else
            {
                MessageBox.Show(message);
            }
        }

        private void ResetInfoStaff()
        {
            teFirstName.Text = "";
            teLastName.Text = "";
            teAddress.Text = "";
            cbeSex.SelectedIndex = 0;
            teTel.Text = "";
            teEmail.Text = "";
            teCMND.Text = "";
            deBirth.EditValue = DateTime.Now;
            deStartDate.EditValue = DateTime.Now;
            cbeStatus.SelectedIndex = 0;
            btnAdd.Enabled = true;
            btnEdit.Enabled = false;
        }

        private void gvStaffs_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            teStaffID.Text = gvStaffs.GetRowCellValue(gvStaffs.GetSelectedRows()[0],gcStaffID).ToString();
            if (gvStaffs.GetRowCellValue(gvStaffs.GetSelectedRows()[0], gcName) != null)
            {
                string[] name = gvStaffs.GetRowCellValue(gvStaffs.GetSelectedRows()[0], gcName).ToString().Split(' ');
                teFirstName.Text = name[name.Length-1];
                string lastName="";
                for (int i = 0; i < name.Length - 1; i++)
                {
                    lastName += name[i];
                    lastName += " ";
                }
                teLastName.Text = lastName.Trim();
            }
            if (gvStaffs.GetRowCellValue(gvStaffs.GetSelectedRows()[0], gcAddress) != null)
                teAddress.Text = gvStaffs.GetRowCellValue(gvStaffs.GetSelectedRows()[0], gcAddress).ToString();
            deBirth.EditValue = Convert.ToDateTime(gvStaffs.GetRowCellValue(gvStaffs.GetSelectedRows()[0], gcDateOfBirth));
            cbeSex.SelectedItem = gvStaffs.GetRowCellValue(gvStaffs.GetSelectedRows()[0], gcSex);
            if (gvStaffs.GetRowCellValue(gvStaffs.GetSelectedRows()[0], gcTel) != null)
                teTel.Text = gvStaffs.GetRowCellValue(gvStaffs.GetSelectedRows()[0], gcTel).ToString();
            if (gvStaffs.GetRowCellValue(gvStaffs.GetSelectedRows()[0], gcEmail) != null)
                teEmail.Text = gvStaffs.GetRowCellValue(gvStaffs.GetSelectedRows()[0], gcEmail).ToString();
            if (gvStaffs.GetRowCellValue(gvStaffs.GetSelectedRows()[0], gcCMND) != null)
                teCMND.Text = gvStaffs.GetRowCellValue(gvStaffs.GetSelectedRows()[0], gcCMND).ToString();
            deStartDate.EditValue = Convert.ToDateTime(gvStaffs.GetRowCellValue(gvStaffs.GetSelectedRows()[0], gcDateOfBirth).ToString());
            cbeStatus.SelectedItem = gvStaffs.GetRowCellValue(gvStaffs.GetSelectedRows()[0], gcStatus).ToString();

            //Nếu đã nghỉ việc thì không cho update Status
            if (cbeStatus.SelectedItem.ToString().Equals("Đã nghỉ việc"))
            {
                cbeStatus.Enabled = false;
            }
            else
            {
                cbeStatus.Enabled = true;
            }

            //Load anh vao pictureedit

            if (gvStaffs.GetRowCellValue(gvStaffs.GetSelectedRows()[0], gcImageProfile) != null)
            {
                // Binary data from MS SQL is received in Base64 format. So you can decode it using the FromBase64String function
                byte[] a = Convert.FromBase64String(gvStaffs.GetRowCellValue(gvStaffs.GetSelectedRows()[0], gcImageProfile).ToString().Replace("\"", ""));
                MemoryStream ms = new MemoryStream(a);
                Image image = Image.FromStream(ms);
                peProfileImage.EditValue = image;
                //can bang hinh anh trong pictureEdit 
                peProfileImage.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
            }
            else
            {
                peProfileImage.EditValue = "";
            }
            btnEdit.Enabled = true;
            btnAdd.Enabled = false;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            StaffDTO staffDTO = new StaffDTO();
            int row = gvStaffs.GetSelectedRows()[0];
            MessageBox.Show(row.ToString());
            staffDTO.StaffID = gvStaffs.GetRowCellValue(gvStaffs.GetSelectedRows()[0], gcStaffID).ToString();
            staffDTO.FirstName = teFirstName.Text;
            staffDTO.LastName = teLastName.Text;
            staffDTO.Address = teAddress.Text;
            staffDTO.Sex = cbeSex.SelectedItem.ToString();
            staffDTO.DateOfBirth = deBirth.DateTime;
            staffDTO.Tel = teTel.Text;
            staffDTO.Email = teEmail.Text;
            staffDTO.CMND = teCMND.Text;
            staffDTO.StartDate = deStartDate.DateTime;
            staffDTO.ImageProfile = peProfileImage.Image;
            staffDTO.Status = cbeStatus.SelectedItem.ToString();

            string message = staffBLL.Update(staffDTO);
            if (message.Equals(MessageAction.Success))
            {
                MessageBox.Show("Cập nhật nhân viên thành công");
                BindingDataStaff();
                gvStaffs.FocusedRowHandle = row;
            }
            else
            {
                MessageBox.Show(message);
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            ResetInfoStaff();
        }

        private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            gcStaff.DataSource = staffBLL.SearchIDName(txtSearch.Text);
        }

        private void btnAddRole_Click(object sender, EventArgs e)
        {
            RoleDTO roleDTO = new RoleDTO();
            roleDTO.RoleName = teRoleName.Text;
            roleDTO.Status = cbeStatusRole.SelectedItem.ToString();

            string message = roleBLL.Insert(roleDTO);
            if (message.Equals(MessageAction.Success))
            {
                MessageBox.Show("Thêm mới quyền thành công");
                BindingDataRole();
                BindingDataComboboxRole();
            }
        }

        private void gcAddStaff_Paint(object sender, PaintEventArgs e)
        {

        }

        private void leRoleName_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void btnEditRole_Click(object sender, EventArgs e)
        {
            RoleDTO roleDTO = new RoleDTO();
            roleDTO.RoleID = Convert.ToInt32(gvRoles.GetRowCellValue(gvRoles.GetSelectedRows()[0], gcRoleID).ToString());
            roleDTO.RoleName = teRoleName.Text;
            roleDTO.Status = cbeStatusRole.SelectedItem.ToString();

            string message = roleBLL.update(roleDTO);
            if (message.Equals(MessageAction.Success))
            {
                MessageBox.Show("Thêm mới quyền thành công");
                BindingDataRole();
                BindingDataComboboxRole();
            }
            else
            {
                MessageBox.Show(message);
            }
        }

        private void panelControl4_Paint(object sender, PaintEventArgs e)
        {

        }

        private void gvRoles_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (gvRoles.GetRowCellValue(gvRoles.GetSelectedRows()[0], gcRoleName) != null)
                teRoleName.Text = gvRoles.GetRowCellValue(gvRoles.GetSelectedRows()[0], gcRoleName).ToString();
            cbeStatusRole.SelectedItem = gvRoles.GetRowCellValue(gvRoles.GetSelectedRows()[0], gcStatusRole).ToString();

            btnAddRole.Enabled = false;
            btnEditRole.Enabled = true;
        }

        private void btnResetRole_Click(object sender, EventArgs e)
        {
            teRoleName.Text = "";
            cbeStatusRole.SelectedIndex=0;
            btnAddRole.Enabled = true;
            btnEditRole.Enabled = false;
        }

        private void teConfirmPassword_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            string v = tePassword.Text;
            if (teStaffID.Text != "")
            {
                UserDTO userDTO = new UserDTO();
                if (tePassword.Text != "")
                {
                    if (tePassword.Text.Equals(teConfirmPassword.Text))
                    {
                        userDTO.StaffID = teStaffID.Text;
                        userDTO.Password = teConfirmPassword.Text;
                        userDTO.RoleID = Convert.ToInt32(leRoleName.EditValue.ToString());
                        userDTO.Status = cbeStatusUser.SelectedItem.ToString();

                        string message = userBLL.Insert(userDTO);
                        if (message.Equals(MessageAction.Success))
                        {
                            MessageBox.Show("Thêm mới người dùng thành công");
                            BindingDataUser();
                        }
                        else
                        {
                            MessageBox.Show(message);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Mật khẩu nhập lại không khớp");
                    }
                }
                else
                {
                    MessageBox.Show("Mời bạn nhập mật khẩu");
                }
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn nhân viên để tạo người dùng");
            }
        }

        private void gvUsers_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            teStaffID.Text = gvUsers.GetRowCellDisplayText(gvUsers.GetSelectedRows()[0],gcStaffIDUser).ToString();
            leRoleName.Text = gvUsers.GetRowCellDisplayText(gvUsers.GetSelectedRows()[0], gcRoleName).ToString();
            leRoleName.ClosePopup();
            cbeStatusUser.SelectedItem = gvUsers.GetRowCellDisplayText(gvUsers.GetSelectedRows()[0], gcStatusUser).ToString();

            btnAddUser.Enabled = false;
            btnEditUser.Enabled = true;
        }

        private void btnResetUser_Click(object sender, EventArgs e)
        {
            teStaffID.Text = "";
            tePassword.Text = "";
            teConfirmPassword.Text = "";
            leRoleName.EditValue = leRoleName.Properties.GetDataSourceValue(leRoleName.Properties.ValueMember, 0);
            leRoleName.ClosePopup();
            cbeStatusUser.SelectedIndex = 0;

            btnAddUser.Enabled = true;
            btnEditUser.Enabled = false;
        }

        private void frmAddStaff_Load(object sender, EventArgs e)
        {
            leRoleName.Properties.PopupWidth = 126;
            leRoleName.EditValue = leRoleName.Properties.GetDataSourceValue(leRoleName.Properties.ValueMember, 0);
            leRoleName.ClosePopup();
        }

        private void btnEditUser_Click(object sender, EventArgs e)
        {
            UserDTO userDTO = new UserDTO();
            if (tePassword.Text != "")
            {
                if (tePassword.Text.Equals(teConfirmPassword.Text))
                {
                    userDTO.StaffID = teStaffID.Text;
                    userDTO.UserID = Convert.ToInt32(gvUsers.GetRowCellValue(gvUsers.GetSelectedRows()[0], gcUserID));
                    userDTO.Password = teConfirmPassword.Text;
                    userDTO.RoleID = Convert.ToInt32(leRoleName.EditValue.ToString());
                    userDTO.Status = cbeStatusUser.SelectedItem.ToString();
                    string message = userBLL.Update(userDTO);
                    if (message.Equals(MessageAction.Success))
                    {
                        MessageBox.Show("Cập nhật người dùng thành công");
                        BindingDataUser();
                    }
                    else
                    {
                        MessageBox.Show(message);
                    }
                }
                else
                {
                    MessageBox.Show("Mật khẩu nhập lại không khớp");
                }

            }
            else
            {
                MessageBox.Show("Mời bạn nhập mật khẩu");
            }
            
        }
    }
}
